/**
 * v8class_instance_manager.h
 *                  Declaration for V8ClassInstanceManager class.
 * Version:         1.0
 *
 * Copyright (c) Bal Bahadur Chettri
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided 
 * that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this list of conditions and the 
 * following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list 
 * of conditions and the following disclaimer in the documentation and/or other materials provided with the 
 * distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Last Updated: Dec 10 2011
 */
#ifndef __V8CLASS_INSTANCE_MANAGER_H
 #define __V8CLASS_INSTANCE_MANAGER_H

#include <map> // std::map

/** ForceDestroyCallback callback function is called when an object needs to be destroyed
    forcefully since v8 engine is out of scope and did not garbage collect */
typedef void (* ForceDestroyCallback) (void *instance);


/** V8ClassInstanceManager class manages the native instances by adding extra mechanism to free
    objects allocated on heap when v8 garbage collection may not work as expected. */
class V8ClassInstanceManager {
private:
    /** Map of native instance and ForceDestroyCallback callback */
	static std::map<void *,ForceDestroyCallback> _instances;
    
public:
    /** Adds an instance to the instance manager */
	static void AddInstance (void *instance, ForceDestroyCallback callback) {
		_instances[instance] = callback;
	}
    
    /** Removes an instance from the instance manager */
	static void RemoveInstance (void *instance) {
		_instances.erase ( _instances.find(instance) );
	}
    
    /** Removes all instances by forcibly destroying them. This should be called usually on program
     termination or when certain context is closed. */
	static void RemoveAllInstances () {
        std::map<void *,ForceDestroyCallback>::iterator 
        it = _instances.begin(),
        it_end = _instances.end();
        
        // invoke ForceDestroyCallback for all objects...
		for (; it != it_end; it++) {
			it->second (it->first);
		}
        
        // clean the map
		_instances.clear ();
	}
};

#endif /* __V8CLASS_INSTANCE_MANAGER_H */
